package cn.dengta.webapp.user.entity;

import java.io.Serializable;

import cn.dengta.common.model.Status;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.X;

/**
 * 通知规则配置对象
 */
@Getter
@Setter
@Accessors(chain = true)
public class NoticeConfig implements Serializable {

	private static final long serialVersionUID = 1L;
	//
	/** 访问我的 */
	protected Integer visit;
	/** 喜欢我的 */
	protected Integer follow;
	/** 私信我的 */
	protected Integer pm;
	/** 系统通知 */
	protected Integer system;

	public void validate() {
		X.use(visit, v -> this.setVisit(Status.valueOf(visit)));
		X.use(follow, v -> this.setFollow(Status.valueOf(follow)));
		X.use(pm, v -> this.setPm(Status.valueOf(pm)));
		X.use(system, v -> this.setSystem(Status.valueOf(system)));
	}

	public static NoticeConfig init() {
		NoticeConfig cfg = new NoticeConfig();
		cfg.visit = Status.YES.value;
		cfg.follow = Status.YES.value;
		cfg.pm = Status.YES.value;
		cfg.system = Status.YES.value;
		return cfg;
	}

	public NoticeConfig init(NoticeConfig input) {
		this.visit = X.expectNotNull(input.visit, visit, Status.YES.value);
		this.follow = X.expectNotNull(input.follow, follow, Status.YES.value);
		this.pm = X.expectNotNull(input.pm, pm, Status.YES.value);
		this.system = X.expectNotNull(input.system, system, Status.YES.value);
		return this;
	}

	public boolean visitOpen() {
		return Status.asResult(visit);
	}

	public boolean followOpen() {
		return Status.asResult(follow);
	}

	public boolean openForFollowOrVisit(boolean likeOrVisit) {
		return likeOrVisit ? followOpen() : visitOpen();
	}

	public boolean pmOpen() {
		return Status.asResult(pm);
	}

	public boolean systemOpen() {
		return Status.asResult(system);
	}

}
